<?php
     /// simulateur
    add_action('admin_init','media_meta_init');  // mbo_meta_init   media_meta_init

    function media_meta_init()
    {
        $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
    	wp_enqueue_style('content-media-meta-category-css', LFDH_THEME_PATH_FUNCTIONS . '/custom_meta/content-media-meta-category-css.css');
        $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
         // check for a template type
    	if ($template_file != 'page-lesprit.php' && $template_file != 'page-identite.php' && $template_file != 'page-expertises.php'&& $template_file != 'page-newsletter.php'&& $template_file != 'page-text.php'&& $template_file != 'page-ref.php')
    	{
           foreach (array('post','page') as $type) 
        	{
        		add_meta_box('media_category_meta', 'Media category', 'media_meta_setup', $type, 'normal', 'high');
        	}
        }
    	// add a callback function to save any data a user enters in
    	add_action('save_post','media_meta_save');
    }

    function media_meta_setup()
    {
    	global $post;
    	// using an underscore, prevents the meta variable
    	// from showing up in the custom fields section
    	$meta = get_post_meta($post->ID,'_media_meta',TRUE);
     
    	// instead of writing HTML here, lets do an include
    	include(LFDH_FUNCTIONS . '/custom_meta/content-media-meta-category-php.php');
    	// create a custom nonce for submit verification later
    	echo '<input type="hidden" name="media_meta_noncename" value="' . wp_create_nonce(__FILE__) . '" />';
    }

    add_action('admin_init','custompagemeta_jsscript');
    function custompagemeta_jsscript(){
        wp_enqueue_script('custompagemeta_script', get_template_directory_uri() .'/lib/js/custommeta_jsscript.js',array('jquery'));
    }
    function media_meta_save($post_id) 
    {
    	// authentication checks
     
    	// make sure data came from our meta box
    	if (!wp_verify_nonce($_POST['media_meta_noncename'],__FILE__)) return $post_id;
     
    	// check user permissions
    	if ($_POST['post_type'] == 'page') 
    	{
    		if (!current_user_can('edit_page', $post_id)) return $post_id;
    	}
    	else 
    	{
    		if (!current_user_can('edit_post', $post_id)) return $post_id;
    	}
    	// authentication passed, save data
    	// var types
    	// single: _media_meta[var]
    	// array: _media_meta[var][]
    	// grouped array: _media_meta[var_group][0][var_1], _media_meta[var_group][0][var_2]
    	$current_data = get_post_meta($post_id, '_media_meta', TRUE);	
    	$new_data = $_POST['_media_meta'];
    	media_meta_clean($new_data);
    	if ($current_data) 
    	{
    		if (is_null($new_data)) delete_post_meta($post_id,'_media_meta');
    		else update_post_meta($post_id,'_media_meta',$new_data);
    	}
    	elseif (!is_null($new_data))
    	{
    		add_post_meta($post_id,'_media_meta',$new_data,TRUE);
    	}
    	return $post_id;
    }
?>